using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;
using Common.Utilities;
using Common.Utilities.Dispatcher;
using Workflows.Components.FileManipulation.Properties;

namespace Workflows.Components.FileManipulation
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class FileEnumeatorComponent:WorkflowComponentBase
    {
        #region props
        private FileLoopInstruction _Instruction;
        private FileLoopOutput _Output;

        /// <summary>
        /// 
        /// </summary>
        public string FolderPath
        {
            get { return this._Instruction.FolderPath; }
            set { this._Instruction.FolderPath = value; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string FileNamePattern
        {
            get { return this._Instruction.FileNamePattern; }
            set { this._Instruction.FileNamePattern = value; }
        }
        /// <summary>
        /// 
        /// </summary>
        public SearchOption SearchOption
        {
            get { return this._Instruction.SearchOption; }
            set { this._Instruction.SearchOption = value; }
        }

        public override InstructionBase Instruction
        {
            get { return this._Instruction; }
            set { this._Instruction=(FileLoopInstruction)value; }
        }

        public override ResultBase Output
        {
            get { return this._Output; }
            set { this._Output = (FileLoopOutput) value; }
        }
        #endregion
        /// <summary>
        /// 
        /// </summary>
        public FileEnumeatorComponent():base()
        {
            this.ComponentActivityType = WorkflowActivityType.SimpleActivity;
            this.ComponentCategory = WorkflowComponentCategory.File_Tasks.ToString();
            this.Description = "Search files within a folder based on given pattern";
            this.ComponentIcon = Resources.search;
            this.ComponentImage = Resources.search_folder;
            this.ComponentName = "File loop task";

            this._Instruction = new FileLoopInstruction(this.ComponentID, Guid.Empty, string.Empty, string.Empty, SearchOption.TopDirectoryOnly);
            this._Output=new FileLoopOutput(this.ComponentID,Guid.Empty,null);
        }

        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = base.Serialize(ref xDoc, ref parentNode);

            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FolderPath", this.FolderPath);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FileNamePattern", this.FileNamePattern);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "SearchOption", ((int)this.SearchOption).ToString());

            return xNode;
        }

        public override WorkflowComponentBase Instantiate(XmlNode xNode)
        {
            if (xNode.Attributes.GetNamedItem("Type").Value == this.GetType().FullName)
            {
                FileEnumeatorComponent fileEnum = base.Instantiate(xNode) as FileEnumeatorComponent;

                fileEnum.FolderPath = xNode.Attributes.GetNamedItem("FolderPath").Value;
                fileEnum.FileNamePattern = xNode.Attributes.GetNamedItem("FileNamePattern").Value;
                fileEnum.SearchOption = (SearchOption)int.Parse(xNode.Attributes.GetNamedItem("SearchOption").Value);

                return fileEnum;
            }
            else
            {
                throw new Exception("Invalid type to instantiate");
            }
        }
    }
}
